package com.sicheng.蓝桥.练习题.dp;

import java.util.Scanner;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/17 11:15
 */
public class 没有上司的舞会 {
    //7
    //1
    //1
    //1
    //1
    //1
    //1
    //1
    //1 3
    //2 3
    //6 4
    //7 4
    //4 5
    //3 5
    static int[][] st;
    static boolean[][] g;
    static int n;

    //https://www.acwing.com/solution/content/105019/
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        st = new int[n + 1][2];
        g = new boolean[n + 1][n + 1];
        int a, b;
        for (int i = 0; i < n; i++) {
            st[i + 1][0] = sc.nextInt();
        }
        while (sc.hasNext()) {
            a = sc.nextInt();
            b = sc.nextInt();
            st[a][1]++;
            g[b][a] = true;
        }

        int p = -1;
        for (int i = 1; i < st.length; i++) {
            if (st[i][1] == 0) {
                p = i;
                break;
            }
        }
        int[] dfs = dfs(p);
        System.out.println(Math.max(dfs[0], dfs[1]));
    }

    static int[] dfs(int u) {
        int[] dp = {0, st[u][0]};
        for (int i = 1; i < g[u].length; i++) {
            if (g[u][i]) {
                int[] dfs = dfs(i);
                dp[0] += Math.max(dfs[0], dfs[1]);
                dp[1] += dfs[0];
            }
        }
        return dp;
    }
}
